﻿
//process.on('message', function (message, socket) {
//    if (message === 'socket')
//        console.log("test");
//        //socket.end('Child handled it.')
//})

process.on('message', function (data) {
    const buffer = Buffer.from(data.buffer);
    process_packet(buffer);
});
process.send({ child: 'start' });

function process_packet_cmd04(buff, pos,len) {
    //注意toString 1-5,是取四个字节
            
    var v_deviceid = buff.toString('hex', pos+1, pos+5);
    var v_press = buff.readFloatBE(pos+7);
    v_press = v_press.toFixed(1);
    var v_temp = buff.readFloatBE(pos+11);
    v_temp = v_temp.toFixed(1);
    var v_time = new Date().Format('yyyy-MM-dd hh:mm:ss');
    var data_1 = { temp: v_temp, deviceid: v_deviceid, press: v_press, time: v_time ,clientport:port};
    //console.log(data_1);
    if(callback!=null)
        callback(data_1);
}

function process_packet_cmd01(org, code, cmd_l, time, head) {
    switch (cmd_l) {
        case 0x01://登陆
            break;
        case 0x02://心跳
         
            break;
        case 0x03://测试
            break;
    }
}

function process_packet_cmd02(org, code, cmd_l, time, head) {
    switch (cmd_l) {

        case 0x02: //自动采集//函数处理//收到数据
            {
                var length = head.length;
                console.log(head.toString('ascii', 16, length));
            }
            break;
    }
}

function process_packet_cmd(org, code, cmd_h, cmd_l, time, head) {
    switch(cmd_h)
    {
        case 0x01://功能类
            process_packet_cmd01(org, code, cmd_l, time, head);
            break;
        case 0x02://采集类
            process_packet_cmd02(org, code, cmd_l, time, head);
            break;
        case 0x03://控制类 ,上行无
            break;
        case 0x04://特殊,上行无
            break;
    }
}


function process_packet(data){
    var org   = data.readInt16BE(2);
    //编号
    var code  = data.readInt16BE(4);
    //命令分类
    var cmd_h = data[6];
    //命令编码
    var cmd_l = data[7];
    console.log(cmd_h, cmd_l);
    //时间
    var time  = {};
    time.year = data[8];
    time.mon  = data[9];
    time.day  = data[10];
    time.hour = data[11];
    time.min  = data[12];
    time.sec  = data[13];
    //版本
    var version = data[14];
    //后继指令
    var follow_code = data[15];

    //var body_len = len - 16 ;//16字节头部
    //交给子进程处理
    process_packet_cmd(org,code, cmd_h,cmd_l,time, data);
}

